import pygame
from pygame.locals import *
import time,random
class HeroPlane:
    '''玩家飞机'''
    def __init__(self, screen_temp):
        self.x = 200
        self.y = 450
        self.screen = screen_temp
        self.image = pygame.image.load("./images/me.png")
        self.bullet_list = []

    def display(self):
        '''绘制玩家飞机'''
        # 绘制子弹
        for b in self.bullet_list:
            b.display()
            if b.move():
                self.bullet_list.remove(b)

        self.screen.blit(self.image, (self.x, self.y))

    #移动
    def move_left(self):
        self.x -= 5
        if self.x <= 0:
            self.x = 0

    def move_right(self):
        self.x += 5
        if self.x >= 406:
            self.x = 406

    # 发射子弹
    def fire(self):
        self.bullet_list.append(Bullet_left(self.screen, self.x, self.y))
        self.bullet_list.append(Bullet_right(self.screen, self.x, self.y))

class Bullet_left:
    '''子弹类'''
    def __init__(self, screen_temp, x, y):
        self.x = x + 20
        self.y = y + 5
        self.screen = screen_temp
        self.image = pygame.image.load("./images/f1.png")

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y -= 10
        if self.y <= -20:
            return True
class Bullet_right:
    '''子弹类'''
    def __init__(self, screen_temp, x, y):
        self.x = x+80
        self.y = y+5
        self.screen = screen_temp
        self.image = pygame.image.load("./images/f1.png")

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y -= 10
        if self.y <= -20:
            return True

class EnemyPlane:
    '''敌机类'''
    def __init__(self, screen_temp):
        self.x = random.choice(range(408))
        self.y = -80
        self.screen = screen_temp
        self.image = pygame.image.load("./images/e1.png")
        self.bullet_list = []
    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self, hero):
        self.y += 3
        for bo in hero.bullet_list:
            if bo.x > self.x + 12 and bo.x < self.x + 92 and bo.y > self.y+20 and bo.y < self.y+60:
                hero.bullet_list.remove(bo)
                for i in range(0, 4):
                    self.screen.blit(pygame.image.load('./images/bomb' + str(i) + '.png'), (self.x, self.y))
                return True
        if self.y >= 550:
            return True

class EnemyPlaneBullet:
    def __init__(self, screen_temp, x, y):
        self.x = x+43
        self.y = y+50
        self.screen = screen_temp
        self.image = pygame.image.load("./images/z1.png")

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y += 3.5
        if self.y >= 600 or self.x <= 0 or self.x >= 406:
            return  True

def key_control(hero_temp):
    '''键盘控制'''
    for event in pygame.event.get():
        if event.type == QUIT:
            print("exit()")
            exit()

    pressed_keys = pygame.key.get_pressed()

    if pressed_keys[K_LEFT] or pressed_keys[K_a]:
        print("LEFT.....")
        hero_temp.move_left()

    elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
        print("Right....")
        hero_temp.move_right()

    if pressed_keys[K_SPACE]:
        print("space....")
        hero_temp.fire()

def main():
    '''主程序'''
    # 创建游戏窗口
    screen = pygame.display.set_mode((512,568),0,0)
    # 创建背景
    background = pygame.image.load("./images/bg2.jpg")
    m = -968
    enemylist = []
    enemybullet_list = []
    hero = HeroPlane(screen)
    while True:
        # 绘制画面
        screen.blit(background, (0, m))
        m += 2

        if m >= -200:
            m = -968

        hero.display()
        key_control(hero)

        if random.choice(range(50)) == 7:
            enemylist.append(EnemyPlane(screen))

        for em in enemylist:
            em.display()

            if random.choice(range(30)) == 7:
                enemybullet_list.append(EnemyPlaneBullet(screen, em.x, em.y))

            for b in enemybullet_list:
                b.display()

                if b.move():
                    enemybullet_list.remove(b)
            if em.move(hero):
                enemylist.remove(em)

            for bo in enemybullet_list:

                if bo.x > hero.x + 12 and bo.x < hero.x + 92 and bo.y > hero.y + 20 and bo.y < hero.y + 60:

                    for i in range(0, 4):
                        screen.blit(pygame.image.load('./images/bomb' + str(i) + '.png'), (hero.x, hero.y))
                        screen.blit(pygame.image.load('./images/bomb' + str(i) + '.png'), (em.x, em.y))

                    while True:
                        screen.blit(pygame.image.load('./images/gameover.png'), (131, 175))

                        for event in pygame.event.get():

                            if event.type == QUIT:
                                print("exit()")
                                exit()

                        pygame.display.update()
                        time.sleep(0.03)
        # 更新显示
        pygame.display.update()
        # 定时显示
        time.sleep(0.03)

if __name__ == "__main__":
    '''判断当前是否是主运行程序'''
    main()
